<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" lang="nl">
    <head>
        <meta charset="UTF-8">
        <meta name="author" content="Pieter P">
        <link rel="stylesheet" type="text/css" href="../CSS/main.css">
        <link href='../CSS/roboto.css' rel='stylesheet' type='text/css'>
        <link href='../CSS/icon.css' rel='stylesheet' type='text/css'>
        <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
        <meta name="theme-color" content="#ccc">
        <title>A Beginner's Guide to the ESP8266</title>
    </head>
    <body>
        <nav>
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 48 48" style="position:absolute;top:4px;right: 4px;">
                    <path d="M0 0h48v48h-48z" fill="none"/>
                    <path d="M6 36h36v-4h-36v4zm0-10h36v-4h-36v4zm0-14v4h36v-4h-36z"/>
                </svg>
                <h2>Index</h2>
                <ol>
<li><a href="Chap01 - ESP8266.html">Introduction</a></li>
<li><a href="Chap02 - Hardware.html">Hardware</a></li>
<li><a href="Chap03 - Software.html">Software</a></li>
<li><a href="Chap04 - Microcontroller.html">Microcontroller</a></li>
<li><a href="Chap05 - Network Protocols.html">Network protocols</a></li>
<li><a href="Chap06 - Uploading.html">Uploading</a></li>
<li><a href="Chap07 - Wi-Fi Connections.html">Wi-Fi connections</a></li>
<li><a href="Chap08 - mDNS.html">mDNS</a></li>
<li><a href="Chap09 - Web Server.html">Web servers</a></li>
<li><a href="Chap10 - Simple Web Server.html">Simple web server</a></li>
<li><a href="Chap11 - SPIFFS.html">SPIFFS</a></li>
<li><b>Uploading to the server</b></li>
<li><a href="Chap13 - OTA.html">OTA</a></li>
<li><a href="Chap14 - WebSocket.html">WebSocket</a></li>
<li><a href="Chap15 - NTP.html">NTP</a></li>
<li><a href="Chap16 - Data Logging.html">Data logging</a></li>
<li><a href="Chap17 - Email Notifier.html">Email notifier</a></li>
<li><a href="Chap18 - Advanced.html">Advanced</a></li>
<li><a href="Chap19 - In Conclusion.html">In conclusion</a></li>

                </ol>
                <a href="https://github.com/tttapa/ESP8266/raw/master/A%20Beginner's%20Guide%20to%20the%20ESP8266%20-%20article/PDF/A-Beginner's-Guide-to-the-ESP8266.pdf" title="Download the guide as a PDF document (automatically generated)">Download as PDF</a><br>
                <a href="https://github.com/tttapa/ESP8266/tree/master/Examples" title="Download all example code that's used in this guide">Download examples</a>
            </div>
        </nav>
        <article>

   <h2>Uploading files to the server</h2>
   <div>
        There are scenarios where you may want to upload new files to the server from within a browser, without having to connect to the ESP8266 over USB in order to flash a new SPIFFS image.
   </div>
   <div>
        In this chapter, I'll show you how to use HTML forms and POST requests to upload or edit files to our little ESP server.
   </div>
   <h3>Client: HTML form</h3>
   <div>
        The easiest way to upload files is by using an HTML form, just like in the first server examples, where we used forms to turn on/off LEDs, and to send the login credentials back to the server. If you choose a file input, you automatically get a file picker, and the browser will send the right POST request to the server, with the file attached.
   </div>
   <div>
       <pre><code>&lt;form method="post" enctype="multipart/form-data"&gt;
    &lt;input type="file" name="name"&gt;
    &lt;input class="button" type="submit" value="Upload"&gt;
&lt;/form&gt;</code></pre>
   </div>
   <h3>Server</h3>
   <div>
        In the ESP code, we have to add a handler to our server that handles POST requests to the <i>/upload</i> URI. When it receives a POST request, it sends a status 200 (OK) back to the client to start receiving the file, and then write it to the SPIFFS. When the file is uploaded successfully, it redirects the client to a success page.
   </div>
   <div>
        The relevant new code is found in the
       <pre>setup</pre>
        and the
       <pre>handleFileUpload</pre>
        function.
   </div>
   <div>
       <pre><code><span style="color: #5e6d03;">#include</span> <span style="color: #434f54;">&lt;</span><span style="color: #d35400;">ESP8266WiFi</span><span style="color: #434f54;">.</span><span style="color: #000000;">h</span><span style="color: #434f54;">&gt;</span>
<span style="color: #5e6d03;">#include</span> <span style="color: #434f54;">&lt;</span><span style="color: #d35400;">WiFiClient</span><span style="color: #434f54;">.</span><span style="color: #000000;">h</span><span style="color: #434f54;">&gt;</span>
<span style="color: #5e6d03;">#include</span> <span style="color: #434f54;">&lt;</span><span style="color: #000000;">ESP8266WiFiMulti</span><span style="color: #434f54;">.</span><span style="color: #000000;">h</span><span style="color: #434f54;">&gt;</span>
<span style="color: #5e6d03;">#include</span> <span style="color: #434f54;">&lt;</span><b><span style="color: #d35400;">ESP8266mDNS</span></b><span style="color: #434f54;">.</span><span style="color: #000000;">h</span><span style="color: #434f54;">&gt;</span>
<span style="color: #5e6d03;">#include</span> <span style="color: #434f54;">&lt;</span><b><span style="color: #d35400;">ESP8266WebServer</span></b><span style="color: #434f54;">.</span><span style="color: #000000;">h</span><span style="color: #434f54;">&gt;</span>
<span style="color: #5e6d03;">#include</span> <span style="color: #434f54;">&lt;</span><span style="color: #000000;">FS</span><span style="color: #434f54;">.</span><span style="color: #000000;">h</span><span style="color: #434f54;">&gt;</span>   <span style="color: #434f54;">// Include the SPIFFS library</span>

<span style="color: #000000;">ESP8266WiFiMulti</span> <span style="color: #000000;">wifiMulti</span><span style="color: #000000;">;</span>     <span style="color: #434f54;">// Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'</span>

<b><span style="color: #d35400;">ESP8266WebServer</span></b> <span style="color: #000000;">server</span><span style="color: #000000;">(</span><span style="color: #000000;">80</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>    <span style="color: #434f54;">// Create a webserver object that listens for HTTP request on port 80</span>

<span style="color: #d35400;">File</span> <span style="color: #000000;">fsUploadFile</span><span style="color: #000000;">;</span>              <span style="color: #434f54;">// a File object to temporarily store the received file</span>

<span style="color: #00979c;">String</span> <span style="color: #000000;">getContentType</span><span style="color: #000000;">(</span><span style="color: #00979c;">String</span> <span style="color: #000000;">filename</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span> <span style="color: #434f54;">// convert the file extension to the MIME type</span>
<span style="color: #00979c;">bool</span> <span style="color: #000000;">handleFileRead</span><span style="color: #000000;">(</span><span style="color: #00979c;">String</span> <span style="color: #000000;">path</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>       <span style="color: #434f54;">// send the right file to the client (if it exists)</span>
<span style="color: #00979c;">void</span> <span style="color: #000000;">handleFileUpload</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>                <span style="color: #434f54;">// upload a new file to the SPIFFS</span>

<span style="color: #00979c;">void</span> <span style="color: #5e6d03;">setup</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">begin</span><span style="color: #000000;">(</span><span style="color: #000000;">115200</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>         <span style="color: #434f54;">// Start the Serial communication to send messages to the computer</span>
  <span style="color: #d35400;">delay</span><span style="color: #000000;">(</span><span style="color: #000000;">10</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #00979c;">'\n'</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>

  <span style="color: #000000;">wifiMulti</span><span style="color: #434f54;">.</span><span style="color: #000000;">addAP</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"ssid_from_AP_1"</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"your_password_for_AP_1"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>   <span style="color: #434f54;">// add Wi-Fi networks you want to connect to</span>
  <span style="color: #000000;">wifiMulti</span><span style="color: #434f54;">.</span><span style="color: #000000;">addAP</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"ssid_from_AP_2"</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"your_password_for_AP_2"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <span style="color: #000000;">wifiMulti</span><span style="color: #434f54;">.</span><span style="color: #000000;">addAP</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"ssid_from_AP_3"</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"your_password_for_AP_3"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>

  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"Connecting ..."</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <span style="color: #00979c;">int</span> <span style="color: #000000;">i</span> <span style="color: #434f54;">=</span> <span style="color: #000000;">0</span><span style="color: #000000;">;</span>
  <span style="color: #5e6d03;">while</span> <span style="color: #000000;">(</span><span style="color: #000000;">wifiMulti</span><span style="color: #434f54;">.</span><span style="color: #d35400;">run</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> <span style="color: #434f54;">!=</span> <span style="color: #000000;">WL_CONNECTED</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #434f54;">// Wait for the Wi-Fi to connect</span>
    <span style="color: #d35400;">delay</span><span style="color: #000000;">(</span><span style="color: #000000;">1000</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
    <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">print</span><span style="color: #000000;">(</span><span style="color: #434f54;">++</span><span style="color: #000000;">i</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span> <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">print</span><span style="color: #000000;">(</span><span style="color: #00979c;">' '</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <span style="color: #000000;">}</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #00979c;">'\n'</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">print</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"Connected to "</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><b><span style="color: #d35400;">WiFi</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">SSID</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>              <span style="color: #434f54;">// Tell us what network we're connected to</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">print</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"IP address:\t"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><b><span style="color: #d35400;">WiFi</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">localIP</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>           <span style="color: #434f54;">// Send the IP address of the ESP8266 to the computer</span>

  <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #434f54;">!</span><b><span style="color: #d35400;">MDNS</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">begin</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"esp8266"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span>             <span style="color: #434f54;">// Start the mDNS responder for esp8266.local</span>
    <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"Error setting up MDNS responder!"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <span style="color: #000000;">}</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"mDNS responder started"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>

  <span style="color: #000000;">SPIFFS</span><span style="color: #434f54;">.</span><span style="color: #d35400;">begin</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>                           <span style="color: #434f54;">// Start the SPI Flash Files System</span>

  <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">on</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"/upload"</span><span style="color: #434f54;">,</span> <span style="color: #00979c;">HTTP_GET</span><span style="color: #434f54;">,</span> <span style="color: #000000;">[</span><span style="color: #000000;">]</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span>                 <span style="color: #434f54;">// if the client requests the upload page</span>
    <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #434f54;">!</span><span style="color: #000000;">handleFileRead</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"/upload.html"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>                <span style="color: #434f54;">// send it if it exists</span>
      <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">send</span><span style="color: #000000;">(</span><span style="color: #000000;">404</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"text/plain"</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"404: Not Found"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span> <span style="color: #434f54;">// otherwise, respond with a 404 (Not Found) error</span>
  <span style="color: #000000;">}</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>

  <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">on</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"/upload"</span><span style="color: #434f54;">,</span> <span style="color: #00979c;">HTTP_POST</span><span style="color: #434f54;">,</span>                       <span style="color: #434f54;">// if the client posts to the upload page</span>
    <span style="color: #000000;">[</span><span style="color: #000000;">]</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">{</span> <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">send</span><span style="color: #000000;">(</span><span style="color: #000000;">200</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span> <span style="color: #000000;">}</span><span style="color: #434f54;">,</span>                          <span style="color: #434f54;">// Send status 200 (OK) to tell the client we are ready to receive</span>
    <span style="color: #000000;">handleFileUpload</span>                                    <span style="color: #434f54;">// Receive and save the file</span>
  <span style="color: #000000;">)</span><span style="color: #000000;">;</span>

  <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">onNotFound</span><span style="color: #000000;">(</span><span style="color: #000000;">[</span><span style="color: #000000;">]</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span>                              <span style="color: #434f54;">// If the client requests any URI</span>
    <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #434f54;">!</span><span style="color: #000000;">handleFileRead</span><span style="color: #000000;">(</span><span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">uri</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>                  <span style="color: #434f54;">// send it if it exists</span>
      <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">send</span><span style="color: #000000;">(</span><span style="color: #000000;">404</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"text/plain"</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"404: Not Found"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span> <span style="color: #434f54;">// otherwise, respond with a 404 (Not Found) error</span>
  <span style="color: #000000;">}</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>

  <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">begin</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>                           <span style="color: #434f54;">// Actually start the server</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"HTTP server started"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #000000;">}</span>

<span style="color: #00979c;">void</span> <span style="color: #5e6d03;">loop</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span>
  <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">handleClient</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #000000;">}</span>

<span style="color: #00979c;">String</span> <span style="color: #000000;">getContentType</span><span style="color: #000000;">(</span><span style="color: #00979c;">String</span> <span style="color: #000000;">filename</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #434f54;">// convert the file extension to the MIME type</span>
  <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #000000;">filename</span><span style="color: #434f54;">.</span><span style="color: #d35400;">endsWith</span><span style="color: #000000;">(</span><span style="color: #005c5f;">".html"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #5e6d03;">return</span> <span style="color: #005c5f;">"text/html"</span><span style="color: #000000;">;</span>
  <span style="color: #5e6d03;">else</span> <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #000000;">filename</span><span style="color: #434f54;">.</span><span style="color: #d35400;">endsWith</span><span style="color: #000000;">(</span><span style="color: #005c5f;">".css"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #5e6d03;">return</span> <span style="color: #005c5f;">"text/css"</span><span style="color: #000000;">;</span>
  <span style="color: #5e6d03;">else</span> <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #000000;">filename</span><span style="color: #434f54;">.</span><span style="color: #d35400;">endsWith</span><span style="color: #000000;">(</span><span style="color: #005c5f;">".js"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #5e6d03;">return</span> <span style="color: #005c5f;">"application/javascript"</span><span style="color: #000000;">;</span>
  <span style="color: #5e6d03;">else</span> <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #000000;">filename</span><span style="color: #434f54;">.</span><span style="color: #d35400;">endsWith</span><span style="color: #000000;">(</span><span style="color: #005c5f;">".ico"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #5e6d03;">return</span> <span style="color: #005c5f;">"image/x-icon"</span><span style="color: #000000;">;</span>
  <span style="color: #5e6d03;">else</span> <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #000000;">filename</span><span style="color: #434f54;">.</span><span style="color: #d35400;">endsWith</span><span style="color: #000000;">(</span><span style="color: #005c5f;">".gz"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #5e6d03;">return</span> <span style="color: #005c5f;">"application/x-gzip"</span><span style="color: #000000;">;</span>
  <span style="color: #5e6d03;">return</span> <span style="color: #005c5f;">"text/plain"</span><span style="color: #000000;">;</span>
<span style="color: #000000;">}</span>

<span style="color: #00979c;">bool</span> <span style="color: #000000;">handleFileRead</span><span style="color: #000000;">(</span><span style="color: #00979c;">String</span> <span style="color: #000000;">path</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #434f54;">// send the right file to the client (if it exists)</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"handleFileRead: "</span> <span style="color: #434f54;">+</span> <span style="color: #000000;">path</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #000000;">path</span><span style="color: #434f54;">.</span><span style="color: #d35400;">endsWith</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"/"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #000000;">path</span> <span style="color: #434f54;">+=</span> <span style="color: #005c5f;">"index.html"</span><span style="color: #000000;">;</span>          <span style="color: #434f54;">// If a folder is requested, send the index file</span>
  <span style="color: #00979c;">String</span> <span style="color: #000000;">contentType</span> <span style="color: #434f54;">=</span> <span style="color: #000000;">getContentType</span><span style="color: #000000;">(</span><span style="color: #000000;">path</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>             <span style="color: #434f54;">// Get the MIME type</span>
  <span style="color: #00979c;">String</span> <span style="color: #000000;">pathWithGz</span> <span style="color: #434f54;">=</span> <span style="color: #000000;">path</span> <span style="color: #434f54;">+</span> <span style="color: #005c5f;">".gz"</span><span style="color: #000000;">;</span>
  <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #000000;">SPIFFS</span><span style="color: #434f54;">.</span><span style="color: #d35400;">exists</span><span style="color: #000000;">(</span><span style="color: #000000;">pathWithGz</span><span style="color: #000000;">)</span> <span style="color: #434f54;">||</span> <span style="color: #000000;">SPIFFS</span><span style="color: #434f54;">.</span><span style="color: #d35400;">exists</span><span style="color: #000000;">(</span><span style="color: #000000;">path</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #434f54;">// If the file exists, either as a compressed archive, or normal</span>
    <span style="color: #5e6d03;">if</span> <span style="color: #000000;">(</span><span style="color: #000000;">SPIFFS</span><span style="color: #434f54;">.</span><span style="color: #d35400;">exists</span><span style="color: #000000;">(</span><span style="color: #000000;">pathWithGz</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>                         <span style="color: #434f54;">// If there's a compressed version available</span>
      <span style="color: #000000;">path</span> <span style="color: #434f54;">+=</span> <span style="color: #005c5f;">".gz"</span><span style="color: #000000;">;</span>                                         <span style="color: #434f54;">// Use the compressed verion</span>
    <span style="color: #d35400;">File</span> <span style="color: #000000;">file</span> <span style="color: #434f54;">=</span> <span style="color: #000000;">SPIFFS</span><span style="color: #434f54;">.</span><span style="color: #d35400;">open</span><span style="color: #000000;">(</span><span style="color: #000000;">path</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"r"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>                    <span style="color: #434f54;">// Open the file</span>
    <b><span style="color: #d35400;">size_t</span></b> <span style="color: #000000;">sent</span> <span style="color: #434f54;">=</span> <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #000000;">streamFile</span><span style="color: #000000;">(</span><span style="color: #000000;">file</span><span style="color: #434f54;">,</span> <span style="color: #000000;">contentType</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>    <span style="color: #434f54;">// Send it to the client</span>
    <span style="color: #000000;">file</span><span style="color: #434f54;">.</span><span style="color: #d35400;">close</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>                                          <span style="color: #434f54;">// Close the file again</span>
    <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #00979c;">String</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"\tSent file: "</span><span style="color: #000000;">)</span> <span style="color: #434f54;">+</span> <span style="color: #000000;">path</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
    <span style="color: #5e6d03;">return</span> <span style="color: #00979c;">true</span><span style="color: #000000;">;</span>
  <span style="color: #000000;">}</span>
  <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #00979c;">String</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"\tFile Not Found: "</span><span style="color: #000000;">)</span> <span style="color: #434f54;">+</span> <span style="color: #000000;">path</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>   <span style="color: #434f54;">// If the file doesn't exist, return false</span>
  <span style="color: #5e6d03;">return</span> <span style="color: #00979c;">false</span><span style="color: #000000;">;</span>
<span style="color: #000000;">}</span>

<span style="color: #00979c;">void</span> <span style="color: #000000;">handleFileUpload</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">{</span> <span style="color: #434f54;">// upload a new file to the SPIFFS</span>
  <span style="color: #000000;">HTTPUpload</span><span style="color: #434f54;">&amp;</span> <span style="color: #000000;">upload</span> <span style="color: #434f54;">=</span> <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #000000;">upload</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <span style="color: #5e6d03;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">upload</span><span style="color: #434f54;">.</span><span style="color: #d35400;">status</span> <span style="color: #434f54;">==</span> <span style="color: #000000;">UPLOAD_FILE_START</span><span style="color: #000000;">)</span><span style="color: #000000;">{</span>
    <span style="color: #00979c;">String</span> <span style="color: #000000;">filename</span> <span style="color: #434f54;">=</span> <span style="color: #000000;">upload</span><span style="color: #434f54;">.</span><span style="color: #000000;">filename</span><span style="color: #000000;">;</span>
    <span style="color: #5e6d03;">if</span><span style="color: #000000;">(</span><span style="color: #434f54;">!</span><span style="color: #000000;">filename</span><span style="color: #434f54;">.</span><span style="color: #d35400;">startsWith</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"/"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span> <span style="color: #000000;">filename</span> <span style="color: #434f54;">=</span> <span style="color: #005c5f;">"/"</span><span style="color: #434f54;">+</span><span style="color: #000000;">filename</span><span style="color: #000000;">;</span>
    <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">print</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"handleFileUpload Name: "</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span> <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #000000;">filename</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">fsUploadFile</span> <span style="color: #434f54;">=</span> <span style="color: #000000;">SPIFFS</span><span style="color: #434f54;">.</span><span style="color: #d35400;">open</span><span style="color: #000000;">(</span><span style="color: #000000;">filename</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"w"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>            <span style="color: #434f54;">// Open the file for writing in SPIFFS (create if it doesn't exist)</span>
    <span style="color: #000000;">filename</span> <span style="color: #434f54;">=</span> <span style="color: #00979c;">String</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
  <span style="color: #000000;">}</span> <span style="color: #5e6d03;">else</span> <span style="color: #5e6d03;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">upload</span><span style="color: #434f54;">.</span><span style="color: #d35400;">status</span> <span style="color: #434f54;">==</span> <span style="color: #000000;">UPLOAD_FILE_WRITE</span><span style="color: #000000;">)</span><span style="color: #000000;">{</span>
    <span style="color: #5e6d03;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">fsUploadFile</span><span style="color: #000000;">)</span>
      <span style="color: #000000;">fsUploadFile</span><span style="color: #434f54;">.</span><span style="color: #d35400;">write</span><span style="color: #000000;">(</span><span style="color: #000000;">upload</span><span style="color: #434f54;">.</span><span style="color: #000000;">buf</span><span style="color: #434f54;">,</span> <span style="color: #000000;">upload</span><span style="color: #434f54;">.</span><span style="color: #000000;">currentSize</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span> <span style="color: #434f54;">// Write the received bytes to the file</span>
  <span style="color: #000000;">}</span> <span style="color: #5e6d03;">else</span> <span style="color: #5e6d03;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">upload</span><span style="color: #434f54;">.</span><span style="color: #d35400;">status</span> <span style="color: #434f54;">==</span> <span style="color: #000000;">UPLOAD_FILE_END</span><span style="color: #000000;">)</span><span style="color: #000000;">{</span>
    <span style="color: #5e6d03;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">fsUploadFile</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span>                                    <span style="color: #434f54;">// If the file was successfully created</span>
      <span style="color: #000000;">fsUploadFile</span><span style="color: #434f54;">.</span><span style="color: #d35400;">close</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>                               <span style="color: #434f54;">// Close the file again</span>
      <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">print</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"handleFileUpload Size: "</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span> <b><span style="color: #d35400;">Serial</span></b><span style="color: #434f54;">.</span><span style="color: #d35400;">println</span><span style="color: #000000;">(</span><span style="color: #000000;">upload</span><span style="color: #434f54;">.</span><span style="color: #000000;">totalSize</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
      <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #000000;">sendHeader</span><span style="color: #000000;">(</span><span style="color: #005c5f;">"Location"</span><span style="color: #434f54;">,</span><span style="color: #005c5f;">"/success.html"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>      <span style="color: #434f54;">// Redirect the client to the success page</span>
      <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">send</span><span style="color: #000000;">(</span><span style="color: #000000;">303</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">}</span> <span style="color: #5e6d03;">else</span> <span style="color: #000000;">{</span>
      <span style="color: #000000;">server</span><span style="color: #434f54;">.</span><span style="color: #d35400;">send</span><span style="color: #000000;">(</span><span style="color: #000000;">500</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"text/plain"</span><span style="color: #434f54;">,</span> <span style="color: #005c5f;">"500: couldn't create file"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">}</span>
  <span style="color: #000000;">}</span>
<span style="color: #000000;">}</span></code></pre>
   </div>
   <div>
        The
       <pre>handleFileUpload</pre>
        function just writes the file attached to the POST request to SPIFFS.
   </div>
   <div>
        <br>
   </div>
   <div>
        If you wan to use other file types as well, you can just add them to the 
       <pre>getContentType</pre>
         function.
   </div>
   <h3>Uploading files</h3>
   <div>
        To upload a new file to the ESP, or to update an existing file, just go to <a href="http://esp8266.local/upload">http://esp8266.local/upload</a>, click the <i>Choose File</i> button, select the file you wish to upload, and click <i>Upload</i>. You can now enter the URL into the URL bar, and open the new file.
   </div>
   <h3>A note on safety</h3>
   <div>
        This example isn't very secure (obviously). Everyone that can connect to the ESP can upload new files, or edit the existing files and insert <a href="https://www.youtube.com/watch?v=L5l9lSnNMxg">XSS code</a>, for example. There's also not a lot of error checking/handling, like checking if there's enough space in the SPIFFS to upload a new file, etc. 
   </div>
   <h3>Advanced example</h3>
   <div>
        The code for these SPIFFS server examples comes (for the most part) from an example written by Hristo Gochkov. You can find it under File &gt; Examples &gt; ESP8266WebServer &gt; FSBrowser. It has a web interface for browsing and editing files in your browser, and has some other nice features as well.
   </div>
    
<hr>
            <div class="back"><a href="Chap11 - SPIFFS.html">← Previous chapter</a></div>
            <div class="next"><a href="Chap13 - OTA.html">Next chapter →</a></div>
            <div class="backArr"><a href="Chap11 - SPIFFS.html"><i class="material-icons">arrow_back</i></a></div>
            <div class="nextArr"><a href="Chap13 - OTA.html"><i class="material-icons">arrow_forward</i></a></div>
        </article>
    </body>
</html>
